define(['./module'],function(module){
	'use strict';


	module.controller('reviewedmemberwithdrawals',serverreviewedmemberwithdrawalsctrl);
	function serverreviewedmemberwithdrawalsctrl($scope,$http,$location){
		var operate=$location.search().operate;
		if(operate!=undefined&&operate=="edit"){
			$scope.title="审核提现申请";
			var id=$location.search().id;
			$scope.reviewed="1";//通审核
			//读取需要审核的提现信息
			$http({
				method:'POST',
				url:'../mall/memberwithdrawals/find',
				data:{
					'id':id
				}
			}).
			success(function(data,status,headers,config){
				if(data.sucflag){
					$scope.applymembername=data.bean.applymembername;
					$scope.applymoney=data.bean.applymoney;
					$scope.applyaccount=data.bean.applyaccount;
					$scope.applytype=data.bean.applytype;
					$scope.updatebtn={show:true};
				}
			}).
			error(function(data,status,headers,config){
				$scope.errors={
					show:true,
					msg:'系统异常'
				}
			});

			$scope.update=function(){
				if($scope.reviewnote==undefined){
					$scope.errors={
						show:true,
						msg:"请输入审核意见"
					}
					return false;
				}
				$http({
					method:'POST',
					url:'../mall/memberwithdrawals/reviewed',
					data:{
						'id':id,
						'reviewnote':$scope.reviewnote,
						'reviewed':$scope.reviewed
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
							show:true,
							msg:'会员提现申请审核成功请等待系统操作转账'
						}
						$location.path('/memberwithdrawalsment');
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});
			}

		}
	}


	module.controller('memberwithdrawals',servermemberwithdrawals);
	function servermemberwithdrawals($scope,$http,$location){

		//通过location中的operate参数区分操作行为
		var operate=$location.search().operate;
		if(operate!=undefined&&operate=="save"){
			//控制保存按钮显示
			$scope.savebtn={show:true};
			$scope.title='添加会员提现申请';
			$scope.applytype="1";
			$scope.applymemberid=$location.search().memberid;
			$scope.applymembername=$location.search().membername;

			if($scope.applymemberid!=undefined){
				/**
				 * 读取可用提现余额
				 */
				$http({
					method:'POST',
					url:'../mall/memberrecharge/find',
					data:{
						'memberid':$scope.applymemberid
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.balance=data.bean.balance;
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});
			}else{
				$scope.errors={
					show:true,
					msg:'参数异常'
				}
			}

			//保存数据方法
			$scope.save=function(){
				if($scope.applymemberid==undefined){
					$scope.errors={
						show:true,
						msg:"请选择提现会员"
					}
					return false;
				}
				if($scope.balance==undefined){
					$scope.errors={
						show:true,
						msg:"会员的帐户余额不足不能提现"
					}
					return false;
				}
				if($scope.applyaccount==undefined){
					$scope.errors={
						show:true,
						msg:"请输入提现转入帐户"
					}
					return false;
				}
				var applymoney=$scope.applymoney;
				if(applymoney==undefined||applymoney==""){
					$scope.errors={
						show:true,
						msg:"请输入充值金额"
					}
					return false;
				}else{
					if(applymoney>$scope.balance){
						$scope.errors={
							show:true,
							msg:"提现金额不能大于帐户余额"
						}
						return false;
					}
				}
				$http({
					method:'POST',
					url:'../mall/memberwithdrawals/save',
					data:{
						'applyaccount':$scope.applyaccount,
						'applymoney':$scope.applymoney,
						'applytype':$scope.applytype,
						'applymemberid':$scope.applymemberid
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
								show:true,
								msg:'会员提现申请成功'
						}
						$location.path('/memberwithdrawalsment');
					}else{
						$scope.errors={
							show:true,
							msg:data.msg
						}
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});

			}
		}

	}

	module.controller('memberwithdrawalsment',servermemberwithdrawalsmentctrl);
	//查询列表数据
	function servermemberwithdrawalsmentctrl($http,$location,$compile,$scope,DTOptionsBuilder,DTColumnBuilder){
		$scope.title="会员提现申请列表";
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=DTOptionsBuilder.newOptions().withOption('ajax',{
			type:'POST',
			url:'../mall/memberwithdrawals/findByPage',
			dataSrc:'data'
		})
		.withOption('processing',true)
		.withOption('paging',true)
		.withOption('serverSide',true)
		.withOption('createdRow',function(row,data,dataIndex){
			$compile(angular.element(row).contents())($scope);
		})
		.withOption('headerCallback', function(header) {
            if (!$scope.headerCompiled) {
                $scope.headerCompiled = true;
                $compile(angular.element(header).contents())($scope);
            }
        })
        .withOption('stateSave', true)
        .withOption('rowCallback',rowCallback)
		.withPaginationType('full_numbers')
		.withLanguageSource('./app/language/chinese.json')
		
		$scope.$on('handleRequest',function(){
			
		});
		
		vm.dtColumns=[
		              DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
		            	  vm.selected[full.id]=false;
		            	  return '<input type="checkbox" ng-model="showCase.selected['+data.id+']" ng-click="showCase.toggleOne(showCase.selected)">';
		              }),
		              DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			          DTColumnBuilder.newColumn('applymembername').withTitle('申请账号').notSortable(),
					  DTColumnBuilder.newColumn('applymoney').withTitle('提现金额').notSortable(),
			DTColumnBuilder.newColumn('applytime').withTitle('申请时间').notSortable(),
			DTColumnBuilder.newColumn('reviewed').withTitle('审核状态').notSortable(),
			DTColumnBuilder.newColumn('istransfer').withTitle('转账状态').notSortable(),
			DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			          DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			          DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable(),
			          DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)];
		function actionHtml(data,type,full,meta){
			return '<button class="btn btn-success" ng-click="memberwithdrawalsreviewed('+data.id+')"><i class="fa fa-edit">审核</i></button>';
		}


		function reloadData(){
			var resetPaging = false;
	        vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}

		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData(){
			var resetPaging=false;
			var searchParams={
				isCustomSearch:1,
				name:''
			};
			if($scope.name!=undefined){
				searchParams.name=$scope.name;
				vm.dtOptions['ajax'].data=searchParams;
				vm.dtInstance.reloadData(callback, resetPaging);
			}
		}

		/**
		 * 表格
		 */
		$scope.searchdataTableCustom=function(){
			searchReloadData();
		}

		/**
		 * 审核
		 */
		//表格中编辑按钮
		$scope.memberwithdrawalsreviewed=function(id,name){
			$location.path('/reviewedmemberwithdrawals').search({'operate':'edit','id':id});
		}

		
		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
						show:true,
						msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
						show:false
				}
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/memberwithdrawals/del.action',
					data:{
						'id':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
								show:true,
								msg:'批量删除成功'
						}
						reloadData();
					}
					
				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;
		}

		function someClickHandler(info) {
	        vm.message = info.id + ' - ' + info.name;
	    }
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
	        $('td', nRow).bind('click', function() {
	            $scope.$apply(function() {
	                vm.someClickHandler(aData);
	            });
	        });
	        return nRow;
		}
	}
	/*=====End Of Find List MemberGroupT=====*/
	
});